# coding: utf-8
import requests
from urllib.parse import urlencode
import pandas as pd

headers = {
    'Host': 'search.damai.cn',
    'origin': 'https://search.damai.cn',
    'content-type': 'application/x-www-form-urlencoded; charset=UTF-8',
    'referer': 'https://search.damai.cn/search.htm?order=1&cty=%E6%97%A0%E9%94%A1',
    'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.119 Safari/537.36',
    'x-requested-with': 'XMLHttpRequest',
}


def loop():
    wuxi_data = []
    for page in range(1, 3):
        data = {
            'keyword': '',
            'cty': '无锡',
            'ctl': '',
            'sctl': '',
            'tsg': 0,
            'st': '',
            'et': '',
            'order': 0,
            'pageSize': 30,
            'currPage': page,
            'tn': ''
        }
        url = 'https://search.damai.cn/searchajax.html?' + urlencode(data)
        rep = requests.get(url, headers=headers)
        pageData = rep.json()['pageData']['resultData']

        for item in pageData:
            categoryname = item['categoryname']
            cityname = item['cityname']
            name = item['name']
            showstatus = item['showstatus']
            showtime = item['showtime']
            venue = item['venue']
            per = {'类别': categoryname, '城市': cityname, '时间': showtime, '状态': showstatus, '地址': venue, '名称': name}
            print(per)
            wuxi_data.append(per)

    df = pd.DataFrame(wuxi_data)
    df.to_excel('wuxi.xlsx')

if __name__ == '__main__':
    loop()
